//
//  GWInitiateWorkSheetV.m
//  ShandsMod
//
//  Created by zh_s on 2019/12/25.
//  Copyright © 2019 zh_s. All rights reserved.
//

#import "GWInitiateWorkSheetV.h"
#import "GWSheetViewCell.h"


@interface GWInitiateWorkSheetV ()<UITableViewDelegate,UITableViewDataSource>{
    NSInteger index;
}
@property (nonatomic, strong) UITableView *mainTableView;
@property (nonatomic, copy) NSString *title;
/**! urlStr*/
@property (nonatomic, copy) NSString *urlStr;

@property (nonatomic, copy) GWSheetResultBlock resultBlock;
@property (nonatomic, copy) GWSheetCancelBlock cancelBlock;
/**! bodyDic*/
@property (nonatomic, strong) NSDictionary *bodyDic;

/**! dataArr*/
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, assign)int page;//页码

/**! type 1=工单类型 2=受理人  4=事业部选择 5=选择酒店*/
@property (nonatomic, copy) NSString *type;

/**! 选中的数组项 都未选中=999*/
@property (nonatomic, assign) NSInteger selectedPlanIndex;
/**! 选中的数组项 都未选中=999*/
@property (nonatomic, assign) NSInteger selectedUserIndex;
/**! 默认选中*/
@property (nonatomic, assign) NSInteger defaultSelValue;

@property (nonatomic, copy) NSString *divisionCodeStr;


@end

@implementation GWInitiateWorkSheetV
+ (void)showDataTableViewWithTitle:(NSString *)title withUrl:(NSString *)urlStr defaultSelValue:(NSInteger )defaultSelValue withBodyDic:(NSDictionary *)bodyDic resultBlock:(GWSheetResultBlock)resultBlock {
    GWInitiateWorkSheetV *dataView = [[GWInitiateWorkSheetV alloc] initWithTitle:title withUrl:urlStr defaultSelValue:defaultSelValue withBodyDic:bodyDic themeColor:nil resultBlock:resultBlock cancelBlock:nil];
    [dataView showWithAnimation:YES];
}

#pragma mark - 初始化自定义选择器
- (instancetype)initWithTitle:(NSString *)title
                      withUrl:(NSString *)urlStr
              defaultSelValue:(NSInteger )defaultSelValue
                  withBodyDic:(NSDictionary *)bodyDic
                   themeColor:(UIColor *)themeColor
                  resultBlock:(GWSheetResultBlock)resultBlock
                  cancelBlock:(GWSheetCancelBlock)cancelBlock {
    if (self = [super init]) {
        self.title = title;
        self.urlStr = urlStr;
        self.resultBlock = resultBlock;
        self.cancelBlock = cancelBlock;
        self.defaultSelValue = defaultSelValue;
        [self initUI];
    }
    return self;
}
#pragma mark - 初始化子视图
- (void)initUI {
    [self.dataArr addObject:@"123124"];
    [self.dataArr addObject:@"3124123"];
    [self.dataArr addObject:@"124124124"];
    [self.dataArr addObject:@"3123124"];
    [self.dataArr addObject:@"12412412412"];
    [self.dataArr addObject:@"312123123"];
    [self.dataArr addObject:@"41241231"];
    [self.dataArr addObject:@"4124123123"];
    [super initUI];

    
    UIImageView *bgImage = [[UIImageView alloc] init];
    [self.alertView addSubview:bgImage];
    bgImage.image = IMAGE_NAME(@"bg_home_view");
    [bgImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.alertView);
    }];
    self.topView.backgroundColor = UIColor.clearColor;
    [self.alertView sendSubviewToBack:bgImage];
    self.titleLabel.text = self.title;
    self.rightBtn.hidden = YES;
    // 添加字符串选择器
    [self.alertView addSubview:self.mainTableView];
}

#pragma mark - 背景视图的点击事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
    [self dismissWithAnimation:NO];
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

#pragma mark - 弹出视图方法
- (void)showWithAnimation:(BOOL)animation {
    //1. 获取当前应用的主窗口
    UIWindow *keyWindow = [[UIApplication sharedApplication] keyWindow];
    [keyWindow addSubview:self];
    if (animation) {
        // 动画前初始位置
        CGRect rect = self.alertView.frame;
        rect.origin.y = SCREEN_HEIGHT;
        self.alertView.frame = rect;
        
        // 浮现动画
        [UIView animateWithDuration:0.3 animations:^{
            CGRect rect = self.alertView.frame;
            rect.origin.y -= DatePicHeight + TopViewHeight;
            self.alertView.frame = rect;
        }];
    }
}

#pragma mark - 关闭视图方法
- (void)dismissWithAnimation:(BOOL)animation {
    // 关闭动画
    [UIView animateWithDuration:0.2 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y += DatePicHeight + TopViewHeight;
        self.alertView.frame = rect;
        
        self.backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        [self.leftImg removeFromSuperview];
        [self.rightBtn removeFromSuperview];
        [self.titleLabel removeFromSuperview];
        [self.lineView removeFromSuperview];
        [self.topView removeFromSuperview];
        [self.alertView removeFromSuperview];
        [self.backgroundView removeFromSuperview];
        [self removeFromSuperview];
        
        self.leftImg = nil;
        self.rightBtn = nil;
        self.titleLabel = nil;
        self.lineView = nil;
        self.topView = nil;
        self.alertView = nil;
        self.backgroundView = nil;
    }];
}

#pragma mark - 取消按钮的点击事件
- (void)clickLeftBtn {
    [self dismissWithAnimation:YES];
    
    if (self.cancelBlock) {
        self.cancelBlock();
    }
}

#pragma mark - 确定按钮的点击事件
- (void)clickRightBtn {
    
    [self dismissWithAnimation:YES];
}
#pragma mark ----- request-工单列表.工单受理人,优先级
- (void)requestTaskUsers {
    
    [ToolsClass createPostHeaderRequestWithURL:self.urlStr withHeadDictionary:nil withBodyDictionary:self.bodyDic withBlock:^(id obj, BOOL success) {
        [self.mainTableView.mj_header endRefreshing];
        if (success) {
            if ([obj[@"code"] integerValue] == 0) {
                [self.dataArr removeAllObjects];
                
                
                [self.mainTableView reloadData];
                
            }else{
                SHOW_TOP_TIPS(obj[@"message"], 0);
            }
        }else{
          
        }

    }];
}
#pragma mark ----- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GWSheetViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.titleLab.text = self.dataArr[indexPath.row];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return CGFLOAT_MIN;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    index = indexPath.row;
    GWSheetViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    NSString *seleStr = self.dataArr[indexPath.row];
    self.resultBlock(seleStr, self.defaultSelValue);
    [self dismissWithAnimation:YES];
}
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}
#pragma mark ----- 懒加载
- (UITableView *)mainTableView {
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, TopViewHeight, SCREEN_WIDTH, DatePicHeight) style:UITableViewStyleGrouped];
        _mainTableView.delegate = self;
        _mainTableView.dataSource= self;
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.rowHeight = 45;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.backgroundColor = UIColor.clearColor;
        [_mainTableView registerClass:[GWSheetViewCell class] forCellReuseIdentifier:@"cell"];
    }
    return _mainTableView;
}

@end
